package com.xq.jltx.frescosimple.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyStringUtils {
	/**
	 * 
	 * @param str
	 *            需要过滤的字符串
	 * @return
	 * @Description:过滤数字以外的字符
	 */
	public static String filterUnNumberForInt(String str) {
		if (str != null) {
			char[] cs = str.toCharArray();
			String type = null;
			if (cs[0] == '-') {
				type = "-";
			} else {
				type = "";
			}
			// 只允数字
			String regEx = "[^0-9]";
			Pattern p = Pattern.compile(regEx);
			Matcher m = p.matcher(str);
			// 替换与模式匹配的所有字符（即非数字的字符将被""替换）
			return type + m.replaceAll("").trim().replace(" ", "");
		} else {
			return null;
		}

	}

	public static boolean saveAsFile(String str, String path, String fileName, String suffix) {
		try {
			File saveFile = new File(path + fileName + "." + suffix);
			if (!saveFile.exists()) {
				File dir = new File(saveFile.getParent());
				dir.mkdirs();
				saveFile.createNewFile();
			}

			FileOutputStream outStream = new FileOutputStream(saveFile);
			outStream.write(str.getBytes());
			outStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}

	public static boolean isBlank(String str) {
		return str == null || str.trim().length() == 0;
	}

	public static boolean isEmpty(String str) {
		return (str == null || str.length() == 0) && isBlank(str);
	}

	public static int length(CharSequence str) {
		return str == null?0:str.length();
	}

	public static String nullStrToEmpty(Object str) {
		return str == null?"":(str instanceof String?(String)str:str.toString());
	}

	public static String capitalizeFirstLetter(String str) {
		if(isEmpty(str)) {
			return str;
		} else {
			char c = str.charAt(0);
			return Character.isLetter(c) && !Character.isUpperCase(c)?(new StringBuilder(str.length())).append(Character.toUpperCase(c)).append(str.substring(1)).toString():str;
		}
	}

	public static String utf8Encode(String str) {
		if(!isEmpty(str) && str.getBytes().length != str.length()) {
			try {
				return URLEncoder.encode(str, "UTF-8");
			} catch (UnsupportedEncodingException var2) {
				throw new RuntimeException("UnsupportedEncodingException occurred. ", var2);
			}
		} else {
			return str;
		}
	}

	public static String utf8Encode(String str, String defultReturn) {
		if(!isEmpty(str) && str.getBytes().length != str.length()) {
			try {
				return URLEncoder.encode(str, "UTF-8");
			} catch (UnsupportedEncodingException var3) {
				return defultReturn;
			}
		} else {
			return str;
		}
	}

	public static String getHrefInnerHtml(String href) {
		if(isEmpty(href)) {
			return "";
		} else {
			String hrefReg = ".*<[\\s]*a[\\s]*.*>(.+?)<[\\s]*/a[\\s]*>.*";
			Pattern hrefPattern = Pattern.compile(hrefReg, 2);
			Matcher hrefMatcher = hrefPattern.matcher(href);
			return hrefMatcher.matches()?hrefMatcher.group(1):href;
		}
	}

	public static String htmlEscapeCharsToString(String source) {
		return isEmpty(source)?source:source.replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&amp;", "&").replaceAll("&quot;", "\"");
	}

	public static String fullWidthToHalfWidth(String s) {
		if(isEmpty(s)) {
			return s;
		} else {
			char[] source = s.toCharArray();

			for(int i = 0; i < source.length; ++i) {
				if(source[i] == 12288) {
					source[i] = 32;
				} else if(source[i] >= '！' && source[i] <= '～') {
					source[i] -= 'ﻠ';
				} else {
					source[i] = source[i];
				}
			}

			return new String(source);
		}
	}

	public static String halfWidthToFullWidth(String s) {
		if(isEmpty(s)) {
			return s;
		} else {
			char[] source = s.toCharArray();

			for(int i = 0; i < source.length; ++i) {
				if(source[i] == 32) {
					source[i] = 12288;
				} else if(source[i] >= 33 && source[i] <= 126) {
					source[i] += 'ﻠ';
				} else {
					source[i] = source[i];
				}
			}

			return new String(source);
		}
	}

	public static String sqliteEscape(String keyWord) {
		keyWord = keyWord.replace("/", "//");
		keyWord = keyWord.replace("\'", "\'\'");
		keyWord = keyWord.replace("[", "/[");
		keyWord = keyWord.replace("]", "/]");
		keyWord = keyWord.replace("%", "/%");
		keyWord = keyWord.replace("&", "/&");
		keyWord = keyWord.replace("_", "/_");
		keyWord = keyWord.replace("(", "/(");
		keyWord = keyWord.replace(")", "/)");
		return keyWord;
	}
}
